package com.learn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.learn.common.Response;
import com.learn.entity.Customer;
import com.learn.entity.CustomerExample;
import com.learn.mybatis.mapper.CustomerMapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
@RequiresAuthentication
public class UserController {
    @Autowired
    CustomerMapper customerMapper;
    CustomerExample customerExample;
    CustomerExample.Criteria criteria;

    @GetMapping("/customers")
    @RequiresRoles(value = {"admin", "user"}, logical = Logical.OR)
    public Response<List<Customer>> getOne(String query) {
        customerExample = new CustomerExample();
        //        创建规则对象
        criteria = customerExample.createCriteria();
        criteria.andDeletedNotEqualTo("yes");
        JSONObject jsonObj = JSON.parseObject(query);
        List<Customer> list = null;
        if (jsonObj.isEmpty()) {

            list = getAllCustomers();
            return Response.success(list);
        }
        for (String key : jsonObj.keySet()) {
            switch (key) {
                case "maxBalance":
                    list = getCustomerByMaxBalance(jsonObj.getDouble(key));
                    break;
                case "minBalance":
                    list = getCustomerByMinBalance(jsonObj.getDouble(key));
                    break;
                case "cardNumber":
                    list = getCustomerByCardNumber(jsonObj.getString(key));
                    break;
                case "companyName":
                    list = getCustomerByCompanyName(jsonObj.getString(key));
                    break;
                case "companyAddress":
                    list = getCustomerByCompanyAddress(jsonObj.getString(key));
                    break;
                case "business":
                    list = getCustomerByBusiness(jsonObj.getString(key));
                    break;
                case "legalRepName":
                    list = getCustomerByLegalRepName(jsonObj.getString(key));
                    break;
                default:
                    list = getAllCustomers();
            }
        }
        //        创建条件对象

//        criteria.
        return Response.success(list);
    }


    @PostMapping("/customers")
    @RequiresRoles(value = {"admin"})
    public Response<String> addOne(@RequestBody Customer customer) {
        customerMapper.insert(customer);
        return Response.success("添加顾客成功");
    }

    @RequiresRoles(value = {"admin"})
    @PostMapping("/customers/{id}")
    public Response<String> updateCustomer(@RequestBody Customer customer, @PathVariable int id) {
        criteria.andIdEqualTo(id);
        customerMapper.updateByExampleSelective(customer, customerExample);
        return Response.success("更新顾客信息成功");
    }

    //    只有超级管理员能够删除顾客信息
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("/customers/{id}")
    public Response<String> delOne(@PathVariable int id) {
        Customer customer = new Customer();
        customer.setDeleted("yes");
        criteria.andIdEqualTo(id);
        customerMapper.updateByExampleSelective(customer, customerExample);
        return Response.success("删除顾客成功");
    }


    //    精确搜索
    public List<Customer> getCustomerByCardNumber(String cardNumber) {
//        System.out.println(cardNumber);
        this.criteria.andCardNumberEqualTo(cardNumber);

        return customerMapper.selectByExample(customerExample);
    }

    public List<Customer> getCustomerByLegalRepName(String legalRepName) {
        this.criteria.andLegalRepNameEqualTo(legalRepName);
        return customerMapper.selectByExample(customerExample);
    }

    //    模糊搜索
    public List<Customer> getCustomerByCompanyName(String companyName) {
        this.criteria.andCompanyNameLike("%" + companyName + "%");
        return customerMapper.selectByExample(customerExample);
    }

    public List<Customer> getCustomerByCompanyAddress(String companyAddress) {
        this.criteria.andCompanyAddressLike("%" + companyAddress + "%");
        return customerMapper.selectByExample(customerExample);
    }


    public List<Customer> getCustomerByBusiness(String business) {
        this.criteria.andBusinessLike("%" + business + "%");
        return customerMapper.selectByExample(customerExample);
    }

//    大于小于

    public List<Customer> getCustomerByMaxBalance(double maxBalance) {
//        System.out.println(maxBalance);
        this.criteria.andBalanceLessThan(maxBalance);
        return customerMapper.selectByExample(customerExample);
    }

    public List<Customer> getCustomerByMinBalance(double minBalance) {
        this.criteria.andBalanceGreaterThan(minBalance);
        return customerMapper.selectByExample(customerExample);
    }

    public List<Customer> getAllCustomers() {
        return customerMapper.selectByExample(customerExample);
    }

}

